package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;


/**
 * 10-请求对象获取请求头
 */
@WebServlet("/demo14")
public class Demo14Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //getHeader : 根据指定请求头名称获取值
        String userAgent = request.getHeader("User-Agent");
        System.out.println("userAgent = " + userAgent);
        //getHeaders : 根据指定请求头名称获取一组值
        Enumeration<String> headerValues = request.getHeaders("User-Agent");
        while (headerValues.hasMoreElements()) {
            String headerValue = headerValues.nextElement();
            System.out.println("userAgent = " + headerValue);
        }
        //getHeaderNames: 获取所有请求头名称
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            String headerValue = request.getHeader(headerName);
            System.out.println("headerName = " + headerName + " , headerValue = " + headerValue);
        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
